var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',
    port: 3306,
    user: 'root',
    password: 'root',
    database: 'music'

});
var Oid = null;
var insertId = null;

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
// 歌曲列表
router.get('/list',function(req,res,next){
  console.log('req.query',req.query);
  let index = parseInt(req.query.index) || 0;
  let limit = parseInt(req.query.limit) || 0;
  connection.query('SELECT * FROM `music-lists` limit '+index*limit+','+limit,function(err,rows,fw){
    if (err) {
      console.log(err);
    }else{
 console.log(req.query.limit);
      
       console.log('链接成功');
       res.send(rows);
    }
  })
 
})
// 分类路由
router.get('/listspecial',function(req,res,next){
  let msg =req.query.msg || '';
  connection.query('SELECT * FROM `music-lists` WHERE type=?',[msg],function(err,rows,fw){
    if (err) {
      console.log(err);
    }else{
 console.log(req.query.limit);
      
       console.log('链接成功');
       res.send(rows);
    }
  })
 
})
// 登陆路由
router.post('/getlog',function(req,res,next){
  let username = req.body.username //用户名
  let password = req.body.password 
  console.log(username,password)
  connection.query('SELECT * FROM `user_td` WHERE suername=? && password=?',[username,password],function(err,rows,fw){
    if (err) {
      res.send(err);
    }else{
      if(rows != false){
       Oid = rows[0].id;
       console.log(Oid)
      }
      //  console.log(Oid);
       res.send(rows);
    }
  })
 
})
// 注册路由
router.post('/addmusic',function(req,res,next){
  let username = req.body.username //用户名
  let password = req.body.password 
  console.log(username,password)
  connection.query('INSERT INTO `user_td`( `suername`, `password`) VALUES (?,?)',[username,password],function(err,rows,fw){
    if (err) {
      console.log('注册没好');
    }else{
      insertId = rows.insertId
       console.log(rows.insertId);
      console.log('好了');
      res.send(rows)
    }
  }) 
})

router.post('/adddata',function(req,res,next){
    console.log(req.body)
    let myname = req.body.myname;
    let area = req.body.area;
    let headimg = req.body.headimg;
    let introduce = req.body.introduce;
    let log = false;
    let mysqlurl = 'INSERT INTO `song_tb`(`id`, `name`, `introduce`, `area`, `log`, `headimg`) VALUES (?,?,?,?,?,?)'
    let mydatas = [insertId,myname,introduce,area,log,headimg]
  connection.query(mysqlurl,mydatas,function(err,rows,fw){
    if (err) {
      console.log('失败'+err);
    }else{
       console.log(rows);
      console.log('message');
      res.send(rows)
    }
  })
})
//获取自己歌单
router.get('/getsongs',function(req,res,next){
  // let Oid = req.body.Oid
  connection.query('SELECT * FROM `song_tb` WHERE id = ?',[Oid],function(err,rows,fw){
    if (err) {
      console.log('链接失败');
    }else{
 console.log(req.query.limit);
       console.log('链接成功');
       console.log(rows);
       console.log(Oid);
       res.send(rows)
       
    }
  })
 
})
module.exports = router;
